<template>
  <el-dialog
    v-el-drag-dialog
    :title="type === 'add' ? '新增资金科目' : '编辑科目资金'"
    :visible.sync="visible"
    width="500px"
    @close="reset"
  >
    <el-form ref="dataForm" :rules="rules" class="form" :model="query" label-width="95px">
      <el-form-item label="科目类型:" class="merge-item" prop="subjectFlag">
        <el-select v-model="query.subjectFlag" :disabled="type==='edit'" style="width: 100%;" clearable>
          <el-option label="收入" :value="1" />
          <el-option label="支出" :value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="科目名称:" prop="subjectName">
        <el-input v-model.trim="query.subjectName" :maxlength="50" placeholder="请输入" clearable />
      </el-form-item>
      <el-form-item label="备注:">
        <el-input v-model.trim="query.remark" :maxlength="200" placeholder="请输入" clearable />
      </el-form-item>
      <el-form-item label="显示顺序:" prop="showIndex">
        <el-input-number v-model.trim="query.showIndex" :min="0" :max="9999" :precision="0" :controls="false" placeholder="请输入" style="width: 100%;" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">
        关闭
      </el-button>
      <el-button :loading="$loadingPlugin.submit" type="primary" @click="success">
        确认
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { accountSubjectSave } from '@/api'
  export default {
    name: 'CapitalSubject',
    components: {

    },
    data() {
      return {
        visible: false,
        title: null,
        type: 'add',
        rules: {
          subjectName: [{ required: true, message: '请输入科目名称', trigger: 'change' }],
          subjectFlag: [{ required: true, message: '请选择科目类型', trigger: 'change' }],
          showIndex: [{ required: true, message: '请输入显示顺序', trigger: 'blur' }]
        },
        query: {
          subjectCode: null,
          showIndex: 0,
          remark: null,
          subjectName: null,
          subjectFlag: null
        }
      }
    },
    methods: {
      open(param) {
        return new Promise(resolve => {
          this.resolve = resolve
          this.visible = true
          if (param) {
            this.type = 'edit'
            this.query = { ...this.query, ...param }
          }
        })
      },
      success() {
        this.$refs.dataForm.validate(valid => {
          if (!valid) return
          this.submit()
        })
      },
      async submit() {
        await accountSubjectSave(this.query)
        this.visible = false
        this.resolve()
        this.$notify({
          title: '提示',
          message: '操作成功',
          type: 'success',
          duration: 2000
        })
      },
      reset() {
        this.type = 'add'
        this.query = {
          subjectCode: null,
          showIndex: 0,
          remark: null,
          subjectName: null,
          subjectFlag: null
        }
        this.$nextTick(() => {
          this.$refs.dataForm.clearValidate()
        })
      }
    }

  }
</script>

<style lang="scss" scoped>

</style>

